﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;



namespace AimGameLibrary.GameObject
{
    public enum Relational
    {
        Equality,
        Inequality,
        LessThan,
        GreaterThan,
        LessThanOrEqualTo,
        GreaterThanOrEqualTo
    }

    public class GameIfelse : BaseGameObject
    {
        //public String valueName1;
        //public String valueName2;
        
        //BaseGameObject ValueObject1;
        //BaseGameObject ValueObject2;

        Relational myrelational;
        StateKey mystatekey;

        public List<String> ListNameIf;
        public List<String> ListNameElse;

        public List<BaseGameObject> ListObjectUpdateIf;
        public List<BaseGameObject> ListObjectUpdateElse;

        public GameIfelse()
        {
            ListNameIf = new List<string>();
            ListNameElse = new List<string>();
            ListObjectUpdateIf = new List<BaseGameObject>();
            ListObjectUpdateElse = new List<BaseGameObject>();
        }

        public override void LoadData(Identification getdata, int IndexData, NodeData node)
        {
            SetupRelational((String)getdata.Items.GetValue(IndexData + 1));
            SetupResult((String)getdata.Items.GetValue(IndexData + 2));

            Listnameparent.Add((String)getdata.Items.GetValue(IndexData + 3));
            Listnameparent.Add((String)getdata.Items.GetValue(IndexData + 4));

            int count = (int)getdata.Items.GetValue(IndexData + 5);
            if (count > 0)
            {
                String Temp = (String)getdata.Items.GetValue(IndexData + 6);
                for (int i = 0; i < count; i++)
                    ListNameIf.Add(Script.GetName(Temp, count, i));
            }
            count = (int)getdata.Items.GetValue(IndexData + 7);
            if (count > 0)
            {
                String Temp = (String)getdata.Items.GetValue(IndexData + 8);
                for (int i = 0; i < count; i++)
                    ListNameElse.Add(Script.GetName(Temp, count, i));
            }
        }

        void SetupRelational(String input)
        {
            if(input == "==")
                myrelational = Relational.Equality;
            else if(input == "!=")
                myrelational = Relational.Inequality;
            else if(input == "<")
                myrelational = Relational.LessThan;
            else if(input == ">")
                myrelational = Relational.GreaterThan;
            else if(input == "<=")
                myrelational = Relational.LessThanOrEqualTo;
            else if(input == ">=")
                myrelational = Relational.GreaterThanOrEqualTo;
        }
       
        void SetupResult(String input)
        {
            if (input == StateKey.IsDown.ToString())
                mystatekey = StateKey.IsDown;
            else if (input == StateKey.IsUp.ToString())
                mystatekey = StateKey.IsUp;
            else if (input == StateKey.Pressed.ToString())
                mystatekey = StateKey.Pressed;
            else if (input == StateKey.Release.ToString())
                mystatekey = StateKey.Release;
        }

        public override void UpdateGameTime(Microsoft.Xna.Framework.GameTime gametime)
        {
            base.UpdateGameTime(gametime);
            if(Logic())
                foreach (BaseGameObject basegame in ListObjectUpdateIf)
                    basegame.UpdateGameTime(gametime);
            else foreach (BaseGameObject basegame in ListObjectUpdateElse)
                    basegame.UpdateGameTime(gametime);
        }

        public override void UpdateTime(TimeSpan timespan)
        {
            base.UpdateTime(timespan);
            if(Logic())
                foreach (BaseGameObject basegame in ListObjectUpdateIf)
                    basegame.UpdateTime(timespan);
            else foreach (BaseGameObject basegame in ListObjectUpdateElse)
                    basegame.UpdateTime(timespan);
        }

        bool Logic()
        {
            if (this.Listparent.Count == 1)
            {
                if (this.Listparent[0] != null)
                    return Selectcase(this.Listparent[0]);
                else return false;
            }
            else if (this.Listparent.Count == 2)
            {
                if (this.Listparent[0] != null && this.Listparent[1] != null)
                    return Selectcase(this.Listparent[0], this.Listparent[1]);
                else return false;
            }
            else return false;
        }

        bool Selectcase(BaseGameObject input1, BaseGameObject input2)
        {
            float getValue1 = ConvertType(input1);
            float getValue2 = ConvertType(input2);

            if (myrelational == Relational.Equality && getValue1 == getValue2)
                    return true;
            else if (myrelational == Relational.Inequality && getValue1 != getValue2)
                    return true;
            else if (myrelational == Relational.LessThan && getValue1 < getValue2)
                    return true;
            else if (myrelational == Relational.GreaterThan && getValue1 > getValue2)
                    return true;
            else if (myrelational == Relational.LessThanOrEqualTo && getValue1 <= getValue2)
                    return true;
            else if (myrelational == Relational.GreaterThanOrEqualTo && getValue1 >= getValue2)
                    return true;
            else return false;
        }

        float ConvertType(BaseGameObject input)
        {
            if (input.GetType() == typeof(GameBoolean))
                return Convert.ToSingle(((GameBoolean)input).MybooleaN);
            else if (input.GetType() == typeof(GameCollision))
                return Convert.ToSingle(((GameCollision)input).MybooleaN);
            else if (input.GetType() == typeof(GameFloat))
                return ((GameFloat)input).MyfloaT;
            else return 0;
        }

        bool Selectcase(BaseGameObject input)
        {
            if (input.GetType() == typeof(GameKeyController))
            {
                if (myrelational == Relational.Equality && ((GameKeyController)input).MystatekeY == mystatekey)
                        return true;
                else if (myrelational == Relational.Inequality && ((GameKeyController)input).MystatekeY != mystatekey)
                        return true;
                else return false;
            }
            else if (input.GetType() == typeof(GameButtonController))
            {
                if (myrelational == Relational.Equality && ((GameButtonController)input).MystatebuttonS == mystatekey)
                    return true;
                else if (myrelational == Relational.Inequality && ((GameButtonController)input).MystatebuttonS != mystatekey)
                    return true;
                else return false;
            }
            else return false;
        }

    }
}
